

#include "actionInspector.h"
#include "action.h"
#include "renderer.h"

ActionInspector::ActionInspector(){

    bOpen=false;
    bPermanent=true;
    initialLocation=location;
    tabWidth=350;
    listHeight=64;
    listWidth=64;
    listDisplayMode=4;
    listColumns=4;
    level=0;

    textureID="icon_ball";
    }

ActionInspector::~ActionInspector(){}


void ActionInspector::setup(){
ListButton::setup();

}

void ActionInspector::update(double deltaTime){

        if (listButton.size()<renderer->actionList.size()){
            assembleList();
        }

}

void ActionInspector::mouseOver(){

ListButton::mouseOver();
}


void ActionInspector::drawPlane(){

    Button::drawPlane();

    renderer->setupShading("color");
    glColor3f(0.4,0.4,0.4);
    if (bOpen){
        glBegin(GL_QUADS);
            glTexCoord2f(0.0,0.0);
            glVertex3f(scale.x,0.0,0.0);
            glTexCoord2f(1.0,0.0);
            glVertex3f(scale.x+tabWidth,0.0,0.0);
            glTexCoord2f(1.0,1.0);
            glVertex3f(scale.x+tabWidth,renderer->screenY,0.0);
            glTexCoord2f(0.0,1.0);
            glVertex3f(scale.x,renderer->screenY,0.0);
        glEnd();
    }

}


void ActionInspector::mouseDrag(){}

void ActionInspector::clickedLeft(){

    if (!bOpen){
        initialLocation=location;
        location.x-=tabWidth;
        bOpen=true;
        for (uint i=0;i<listButton.size();i++){
            listButton[i]->location.x-=tabWidth;
            listButton[i]->initialLocation.x=listButton[i]->location.x;
            }
        assembleList();
    }
    else{
        for (uint i=0;i<listButton.size();i++)
            listButton[i]->location.x+=initialLocation.x-location.x;
        location=initialLocation;
        bOpen=false;
    }
}


void ActionInspector::assembleList(){

        cout << "creating list..." << endl;

        std::map <std::string, Action*>::iterator it;
        int i=0;

        listButton.clear();
        for ( it=renderer->actionList.begin() ; it != renderer->actionList.end(); it++ ){


            listButton.push_back(it->second);

            listButton[i]->level=level+1;
            listButton[i]->bDrawName=true;
            listButton[i]->color=Vector4f(1.0,1.0,0.0,1.0);
            //actions are just set to be visible, not actually created!
            listButton[i]->bHidden=false;
            listButton[i]->bDragable=true;

            placeButton(i,i);
            //set this because we want to drag buttons around!
            listButton[i]->initialLocation=listButton[i]->location;
            i++;
            }

    cout << "our Button list is: "<< listButton.size() <<" elements long..." << endl;

    for (int i=0;i<(int)renderer->buttonList.size();i++){
            cout << "list has: " << renderer->buttonList[i]->name << endl;
        }
    for (int i=0;i<(int)listButton.size();i++){
            cout << "button has: " << listButton[i]->name << endl;
        }

}


void ActionInspector::clickedRight(){}
void ActionInspector::focusClick(){

ListButton::focusClick();
}

void ActionInspector::deselect(int depth){

//ListButton::deselect(depth);
}

void ActionInspector::create(){renderer->addButton(this);}
